package com.yanqu.road.server.cmd.union.war;

import com.yanqu.road.logic.union.UnionWarManger;
import com.yanqu.road.pb.unionwar.UnionWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.union.UnionWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code= Protocol.S_UNION_WAR_DONATE, desc = "商会战捐赠")
public class DonateSilverCmd extends AbstractCommand {

    private BigInteger silver;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarProto.DonateSilverReqMsg reqMsg = UnionWarProto.DonateSilverReqMsg.parseFrom(packet.getBytes());
        silver = new BigInteger(reqMsg.getSilver());
        if (silver.compareTo(BigInteger.ZERO) < 0) {
            return false;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        if (!UnionWarManger.isDonateTime()) {
            ret = GameErrorCode.E_UNION_WAR_NO_INSPIRE_TIME;
            return;
        }
        ret = UnionWarMgr.donateSilver(player, silver);
    }

    @Override
    public void sendData(GamePlayer player) {
        UnionWarProto.DonateSilverRespMsg.Builder respMsg = UnionWarProto.DonateSilverRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_UNION_WAR_DONATE, respMsg);
    }
}
